package com.mdd.admin.controller.student;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IStudentService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.student.StudentCreateValidate;
import com.mdd.admin.validate.student.StudentUpdateValidate;
import com.mdd.admin.validate.student.StudentSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.student.StudentDetailVo;
import com.mdd.admin.vo.student.StudentListedVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.student.Student;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("api/student")
@Api(tags = "学生管理")
public class StudentController {

    @Resource
    IStudentService iStudentService;

    @GetMapping("/list")
    @ApiOperation(value="学生列表")
    public AjaxResult<PageResult<StudentListedVo>> list(@Validated PageValidate pageValidate,
                                                        @Validated  StudentSearchValidate searchValidate) {
        PageResult<StudentListedVo> list = iStudentService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="学生详情")
    public AjaxResult<StudentDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        StudentDetailVo detail = iStudentService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "学生新增")
    @PostMapping("/add")
    @ApiOperation(value="学生新增")
    public AjaxResult<Object> add(@Validated @RequestBody StudentCreateValidate createValidate) {
        iStudentService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "学生编辑")
    @PostMapping("/edit")
    @ApiOperation(value="学生编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody StudentUpdateValidate updateValidate) {
        iStudentService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "学生删除")
    @PostMapping("/del")
    @ApiOperation(value="学生删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iStudentService.del(idValidate.getId());
        return AjaxResult.success();
    }


    public static int[][] charToPixelMatrix(char c, int width, int height, Font font) {
        // 创建一个新的BufferedImage
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // 获取Graphics2D对象来绘制字符
        Graphics2D g2d = image.createGraphics();
        // 设置背景颜色为透明
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();

        // 设置绘制属性
        g2d.setFont(font);
        g2d.setColor(Color.BLACK); // 字符颜色
        g2d.drawString("" + c, 0, font.getSize()); // 在图像上绘制字符

        // 遍历图像中的每个像素
        int[][] pixelMatrix = new int[height][width];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 获取每个像素的颜色
                Color color = new Color(image.getRGB(x, y));
                // 将颜色转换为灰度值，这里简单地用alpha值代表像素的亮度
                pixelMatrix[y][x] = color.getAlpha();
            }
        }
        g2d.dispose();
        return pixelMatrix;
    }

// https://blog.csdn.net/qq_37598011/article/details/86242116
    public static void printHZK(String character) throws IOException {
        int sum = 32;
        byte[] data = character.getBytes("GB2312");
        byte iHigh, iLow;// 高8位，低8位
        if (data.length > 1) {
            iLow = (byte) (data[0]);
            iHigh = (byte) (data[1]);
        } else {
            iLow = (byte) (data[0]);
            iHigh = 0;
        }
        int IOffset;// 文件偏移量
        IOffset = (94 * ((iLow & 0xff) - 0xA1) + ((iHigh & 0xff) - 0xA1)) * sum;// 计算偏移字节
//        File f = new File("D:\\HZK16");
        URL resource = Student.class.getClassLoader().getResource("HZK16");
        String path = resource.getPath();
        File file = new File(path);
        InputStream fis = new FileInputStream(file);
        fis.read(new byte[IOffset]);// 偏移一个量纲
        byte mat[] = new byte[sum];// 连续读入
        fis.read(mat);
        fis.close();
        // 画出点阵图
        int i, j, k;
        OutputStream out=new FileOutputStream(new File("D:\\a.txt"),true);
        //创建字节流缓冲区，加快写出速度
        BufferedOutputStream bout=new BufferedOutputStream(out);
        //创建字符输出流对象
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(bout, "utf-8"));
        for (i = 0; i < sum / 2; i++) /* 16x16点阵汉字，一共有16行 */
        {
            for (j = 0; j < 2; j++) { /* 横向有2个字节，循环判断每个字节的 */
                for (k = 0; k < 8; k++) {
                    if ((mat[i * 2 + j] & (0x80 >> k)) >= 1) { // 判断一个二进制位上为 1 则打印
                        System.out.print("■ ");
                        bw.write("■ ");
                    } else {
                        System.out.print("□ ");
                        bw.write("□ ");
                    }
                }
            }
            System.out.println();
            bw.write("\r\n");
        }
        bw.write("\r\n");
        bw.close();
        bout.close();
        out.close();
        System.out.println();
    }


    public static void main2(String[] args) {
        // 示例：将字符 'A' 转换为像素点阵
        char character = '我';
        int width = 16;
        int height = 16;
        Font font = new Font("Courier New", Font.PLAIN, 16);

        int[][] pixelMatrix = charToPixelMatrix(character, width, height, font);

        // 打印像素点阵
        for (int[] row : pixelMatrix) {
            for (int pixel : row) {
                System.out.print(pixel + " ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) throws IOException {
        printHZK("我");
    }


}
